<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>我的购物车</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .back-link {
            background-color: #2196F3;
            color: white;
            padding: 10px 15px;
            text-decoration: none;
            border-radius: 4px;
        }
        .cart-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        .cart-table th, .cart-table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        .cart-table th {
            background-color: #f8f9fa;
        }
        .quantity-input {
            width: 50px;
            text-align: center;
            padding: 5px;
        }
        .update-btn {
            background-color: #2196F3;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
        }
        .remove-btn {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
        }
        .total-section {
            text-align: right;
            font-size: 18px;
            font-weight: bold;
            margin-top: 20px;
        }
        .checkout-btn {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            float: right;
        }
        .empty-cart {
            text-align: center;
            padding: 50px;
            color: #777;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>我的购物车</h1>
        <a href="products.jsp" class="back-link">继续购物</a>
    </div>

    <%
        Map<Integer, Map<String, Object>> cart = (Map<Integer, Map<String, Object>>)session.getAttribute("cart");
        if(cart == null || cart.isEmpty()) {
    %>
    <div class="empty-cart">
        <h2>购物车是空的</h2>
        <p>快去选购商品吧！</p>
    </div>
    <% } else { %>
    <table class="cart-table">
        <thead>
        <tr>
            <th>商品名称</th>
            <th>单价</th>
            <th>数量</th>
            <th>小计</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <%
            double total = 0;
            for(Map.Entry<Integer, Map<String, Object>> entry : cart.entrySet()) {
                Map<String, Object> item = entry.getValue();
                double price = (double)item.get("price");
                int quantity = (int)item.get("quantity");
                double subtotal = price * quantity;
                total += subtotal;
        %>
        <tr>
            <td><%= item.get("name") %></td>
            <td>¥<%= String.format("%.2f", price) %></td>
            <td>
                <form action="updateCart.jsp" method="post" style="display: inline;">
                    <input type="hidden" name="productId" value="<%= item.get("id") %>">
                    <input type="number" name="quantity" value="<%= quantity %>" min="1" class="quantity-input">
                    <button type="submit" class="update-btn">更新</button>
                </form>
            </td>
            <td>¥<%= String.format("%.2f", subtotal) %></td>
            <td>
                <form action="removeFromCart.jsp" method="post" style="display: inline;">
                    <input type="hidden" name="productId" value="<%= item.get("id") %>">
                    <button type="submit" class="remove-btn">删除</button>
                </form>
            </td>
        </tr>
        <% } %>
        </tbody>
    </table>

    <div class="total-section">
        总计: ¥<%= String.format("%.2f", total) %>
    </div>

    <button class="checkout-btn">结算</button>
    <div style="clear: both;"></div>
    <% } %>
</div>
</body>
</html>